﻿using Company.Plc;
using Company.Shell.Interfaces;
using Company.Shell.Models;

namespace Company.Shell.Services
{
    class HardwareMananger : IHardwareMananger
    {
        public bool Initialized { get; private set; }
        private IPlc Plc { get; }

        public HardwareMananger(IPlc plc)
        {
            Plc = plc;
        }

        public void Close()
        {
            Plc.Close();
        }

        public async Task<InitializeResult> InitializeAsync()
        {
            if (Initialized)
                throw new Exception("重复初始化硬件");

            return await Task.Run(() =>
            {
                InitializeResult result = new ();
                bool success = Plc.Init(new PlcConfig());
                if (success)
                {
                    Initialized = true;
                    result.Success = true;
                    result.Message = "PLC连接成功";
                }

                return result;
            });
        }
    }
}
